class ClassTest:
    # 静态变量（类属性）：在class内的，但不在class的方法内的，这就是静态变量
    static_variable = 100

    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart
        self.val2 = 'ABC'  # 实例变量

    # 类属性
    explanation = 'this is my programs'

    # 实例方法（普通方法）
    def normalMethod(self, name):
        print(self.explanation)

        # 类方法,可以访问类属性

    @classmethod
    def classMethod(cls, explanation):
        print(cls.explanation)

    # 静态方法，不可以访问类属性
    @staticmethod
    def staticMethod(explanation):
        print(explanation)


# isinstance(object, classinfo)
# 其中，object为实例对象，classinfo,用于判断object是否为classinfo类型

# issubclass函数方法用于判断参数class是否是类型参数 classinfo 的子类，用法如下：

class A:
    pass


class B(A):
    pass


print(issubclass(B, A))


class A:
    pass


class B(A):
    pass


print(isinstance(A(), A))
print(isinstance(B(), A))

test = ClassTest(666, 777)
print(test.r)
print(test.i)


# 单例模式
class Singleton(object):
    _instance = None

    def __new__(cls, *args, **kw):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
        return cls._instance


class SonClass(Singleton):
    a = 1


one = SonClass()
two = SonClass()
print(one == two)
print(one is two)
print(id(one), id(two))
